#include <plib.h>
#include "Bike_Display_HW_Profile.h"
#include "Bike_Display_4dSystems_Functions.h"
#include "Bike_Display_SystemSetup_Functions.h"
#include "Bike_Display_Bitmap_Definitions.h"
#include <p32xxxx.h>
#include <stdbool.h>
#include "Bike_Display_UART_Functions.h"

// Screen Data
int type;
int revision;
int firmware;
int reserved1;
int reserved2;

// Text data
char current_col;
char current_row;
int  current_color;
char current_font;
char current_orientation;
char max_col;
char max_row;

char SendData[20];
char ReceiveData[20];
unsigned int TouchX;
unsigned int TouchY;

//******************************************************************************************************
void LCDInit(void)
{
    LCDRestart();

    UART1Init();
    
    LCDAutoBaud();        		 // autobaud has to be the first command after reset
    LCDCls();
    LCDBaudRate(128000);  	 // set the initial baudrate to 115.2kbps - fastest supported by uLCD-32PT
    LCDCls();
    // next two commands are required to display images stored on the RAW part of the uSD card
    // see section 3.1.6 in the PICASO-SGC-COMMANDS-SIS
    LCDDisplayControl(IMAGE_FORMAT, NEW);       // set LCD image format to new format, including headers
    LCDDisplayControl(PROTECT_FAT, UNPROTECT);  // set uSD card to unprotected FAT
    LCDDisplayControl(TOUCH_CTRL, ENABLE);      // activate use of touch screen
}

// Reset LCD Display
void LCDRestart(void)
{ 
    PORTClearBits(IOPORT_D, LCDReset);
	DelayMs(100);
	PORTSetBits(IOPORT_D, LCDReset);
	DelayMs(2000);
}

// send AutoBaud command (9600)
void LCDAutoBaud(void)
{
    SendData[0] = AUTOBAUD;
    SendDataBuffer(SendData, 1);
    
    if (!GetDataBuffer(ReceiveData, 1) == ACK)
    {
            DBPRINTF("Command has not been acknowledged");
    }
}

// clear screen
void LCDCls(void)
{
    SendData[0] = CLS;
    SendDataBuffer(SendData, 1);
    if (!GetDataBuffer(ReceiveData, 1) == ACK)
    {
            DBPRINTF("Command has not been acknowledged");
    }
}

// set screen baud rate
void LCDBaudRate(long speed)
{
    UINT    actualDataRate;  
    SendData[0] = BAUDRATE;
    switch (speed)
    {
        case  110 :
            SendData[1] = BAUD_110;
            break;
        case  300 :
            SendData[1] = BAUD_300;
            break;
        case  600 :
            SendData[1] = BAUD_600;
            break;
        case 1200 :
            SendData[1] = BAUD_1200;
            break;
        case 2400 :
            SendData[1] = BAUD_2400;
            break;
        case 4800 :
            SendData[1] = BAUD_4800;
            break;
        case 9600 :
            SendData[1] = BAUD_9600;
            break;
        case 14400 :
            SendData[1] = BAUD_14400;
            break;
        case 19200 :
            SendData[1] = BAUD_19200;
            break;
        case 31250 :
            SendData[1] = BAUD_31250;
            break;
        case 38400 :
            SendData[1] = BAUD_38400;
            break;
        case 56000 :
            SendData[1] = BAUD_56000;
            break;
        case 57600 :
            SendData[1] = BAUD_57600;
            break;
        case 115200 :
            SendData[1] = BAUD_115200;
            break;
        case 128000 :
            SendData[1] = BAUD_128000;
            break;
        case 256000 :
            SendData[1] = BAUD_256000;
            break;
        default   :
            SendData[1] = BAUD_9600;
            speed = 9600;
            break;
    }
    SendDataBuffer(SendData, 2);

    actualDataRate = UARTSetDataRate(UART1, GetPeripheralClock(), speed);
    if (!GetDataBuffer(ReceiveData, 1) == ACK)
    {
            DBPRINTF("Command has not been acknowledged");
    }
}
// set screen mode to value
void LCDDisplayControl(char mode, char value)
{   
    SendData[0] = DISPCONTROL;
    SendData[1] = mode;
    SendData[2] = value;

    if (mode ==  ORIENTATION)
    {
        switch (value)
        {
            case LANDSCAPE :
                current_orientation = IS_LANDSCAPE;
                break;
            case LANDSCAPE_R :
                current_orientation = IS_LANDSCAPE;
                break;
            case PORTRAIT :
                current_orientation = IS_PORTRAIT;
                break;
            case PORTRAIT_R :
                current_orientation = IS_PORTRAIT;
                break;
        }
        LCDSetFont(current_font);
    }
    SendDataBuffer(SendData, 3);
    
    if (!GetDataBuffer(ReceiveData, 1) == ACK)
    {
            DBPRINTF("Command has not been acknowledged");
    }
}

//**************************************************************************************************
// This displays an image on the screen in the NEW FORMAT
// Sent X-pos, Y-pos, Sector Address - This is the recommended way to display images
//****************************************************************************************************
void LCDuSDImage(int x, int y, long s){  
    char S1, S2, S3;
    char X_MSB, X_LSB, Y_MSB, Y_LSB;
    
    X_LSB = x&0x00FF;           //Work out the x position
    X_MSB = (x >> 8); 
    Y_LSB = y&0x00FF;           //Work out the y position
    Y_MSB = (y >> 8);
    
    S1 = (s >> 16)&0x0000FF;    //Work out the sector address
    S2 = (s >> 8)&0x0000FF; 
    S3 = s&0x0000FF;

    SendData[0] = 0x40;          //ext_cmd
    SendData[1] = 0x49;          //Display image
    SendData[2] = X_MSB;         //X position - 2 bytes    
    SendData[3] = X_LSB;
    SendData[4] = Y_MSB;         //Y position - 2 bytes
    SendData[5] = Y_LSB;
    SendData[6] = S1;            //Sector address - 3 bytes
    SendData[7] = S2;
    SendData[8] = S3;
    
    SendDataBuffer(SendData, 9);
    
    if (!GetDataBuffer(ReceiveData, 1) == ACK)
    {
            DBPRINTF("Command has not been acknowledged");
    }
}

// set font size
void LCDSetFont(char mode)
{   
    int w, h, fx = 8, fy = 8;

    SendData[0] = SETFONT;
    SendData[1] = mode;

    current_font = mode;

    if (current_orientation == IS_PORTRAIT)
    {
        w = SIZE_X;
        h = SIZE_Y;
    } 
    else
    {
        w = SIZE_Y;
        h = SIZE_X;
    }

    switch (mode)
    {
        case FONT_5X7 :
            fx = 6;
            fy = 8;
            break;
        case FONT_8X8 :
            fx = 8;
            fy = 8;
            break;
        case FONT_8X12 :
            fx = 8;
            fy = 12;
            break;
        case FONT_12X16 :
            fx = 12;
            fy = 16;
            break;
    }

    max_col = w / fx;
    max_row = h / fy;

    SendDataBuffer(SendData, 2);
    
    if (!GetDataBuffer(ReceiveData, 1) == ACK)
    {
            DBPRINTF("Command has not been acknowledged");
    }
}

// set text mode to opaque or transparent
void LCDTextMode(char mode)
{
    SendData[0] = TEXTMODE;
    SendData[1] = mode;

    SendDataBuffer(SendData, 2);
    
    if (!GetDataBuffer(ReceiveData, 1) == ACK)
    {
            DBPRINTF("Command has not been acknowledged");
    }
}

// draw a text char
void LCDTextChar(char c, char col, char row, int color)
{   
    SendData[0] = TEXTCHAR;

    SendData[1] = c;
    SendData[2] = col;
    SendData[3] = row;

    SendData[4] = (color >> 8) & 0xFF;  // first part of 16 bits color
    SendData[5] = color & 0xFF;  // second part of 16 bits color

    SendDataBuffer(SendData, 6);
    
    if (!GetDataBuffer(ReceiveData, 1) == ACK)
    {
            DBPRINTF("Command has not been acknowledged");
    }
}

// draw a graphic char
void LCDGraphicChar(char c, int x, int y, int color, char width, char height)
{   
    SendData[0] = GRAPHCHAR;

    SendData[1] = c;

    SendData[2] = (x >> 8) & 0xFF;
    SendData[3] = x & 0xFF;

    SendData[4] = (y >> 8) & 0xFF;
    SendData[5] = y & 0xFF;

    SendData[6] = (color >> 8) & 0xFF;  // first part of 16 bits color
    SendData[7] = color & 0xFF;  // second part of 16 bits color

    SendData[8] = width;

    SendData[9] = height;

    SendDataBuffer(SendData, 10);

    if (!GetDataBuffer(ReceiveData, 1) == ACK)
    {
            DBPRINTF("Command has not been acknowledged");
    }
}

// draw a text string
void LCDTextString(char *s, char col, char row, char font, int color)
{   
    int size = strlen(s);
    int i = 0;

    SendData[0] = TEXTSTRING;

    SendData[1] = col;
    SendData[2] = row;

    SendData[3] = font;

    SendData[4] = (color >> 8) & 0xFF;  // first part of 16 bits color
    SendData[5] = color & 0xFF;  // second part of 16 bits color

    for (i=0; i<size; i++) SendData[6+i] = s[i];

    SendData[6+size] = 0;

    SendDataBuffer(SendData, 7 + size);

    if (!GetDataBuffer(ReceiveData, 1) == ACK)
    {
            DBPRINTF("Command has not been acknowledged");
    }
}

// draw a text string
void LCDGraphicString(char *s, int x, int y, char font, int color, char width, char height)
{  
    int size = strlen(s);
    int i = 0;

    SendData[0] = GRAPHSTRING;

    SendData[1] = (x >> 8) & 0xFF;
    SendData[2] = x & 0xFF;

    SendData[3] = (y >> 8) & 0xFF;
    SendData[4] = y & 0xFF;

    SendData[5] = font;

    SendData[6] = (color >> 8) & 0xFF;  // first part of 16 bits color
    SendData[7] = color & 0xFF;  // second part of 16 bits color

    SendData[8] = width;

    SendData[9] = height;

    for (i=0; i<size; i++) SendData[10+i] = s[i];

    SendData[10+size] = 0;

    SendDataBuffer(SendData, 11 + size);

    if (!GetDataBuffer(ReceiveData, 1) == ACK)
    {
            DBPRINTF("Command has not been acknowledged");
    }
}

// draw a pixel
void LCDDrawPixel(int x, int y, int color)
{   
    SendData[0] = PIXEL;

    SendData[1] = (x >> 8) & 0xFF;
    SendData[2] = x & 0xFF;

    SendData[3] = (y >> 8) & 0xFF;
    SendData[4] = y & 0xFF;

    SendData[5] = (color >> 8) & 0xFF;  // first part of 16 bits color
    SendData[6] = color & 0xFF;  // second part of 16 bits color

    SendDataBuffer(SendData, 7);
    
    if (!GetDataBuffer(ReceiveData, 1) == ACK)
    {
            DBPRINTF("Command has not been acknowledged");
    }
}

// draw a rectangle
void LCDDrawRect(int x1, int y1, int x2, int y2, int colour)
{   
    SendData[0] = RECTANGLE;

    SendData[1] = (x1 >> 8) & 0xFF;
    SendData[2] = x1 & 0xFF;

    SendData[3] = (y1 >> 8) & 0xFF;
    SendData[4] = y1 & 0xFF;

    SendData[5] = (x2 >> 8) & 0xFF;
    SendData[6] = x2 & 0xFF;

    SendData[7] = (y2 >> 8) & 0xFF;
    SendData[8] = y2 & 0xFF;

    SendData[9] = (colour >> 8) & 0xFF;  // first part of 16 bits color
    SendData[10] = colour & 0xFF;  // second part of 16 bits color

    SendDataBuffer(SendData, 11);
    
    if (!GetDataBuffer(ReceiveData, 1) == ACK)
    {
            DBPRINTF("Command has not been acknowledged");
    }
}

void MyLCDDisplayValue(int nValue, int nValueOnLCD, bool bColourChanged, bool bNegativeChanged, display_colour eStringColour, screen_position eStringPosition)
{
    int nXPos = 0;
    int nYPos = 0;
    int nSize = 0;
    int nDigits = 0; 
    int nThousand = 0;
    int nHundred = 0;
    int nTen = 0;
    int nSingle = 0;
    int nThousandOnLCD = 0;
    int nHundredOnLCD = 0;
    int nTenOnLCD = 0;
    int nSingleOnLCD = 0;
    bool bBlankThousand = false;
    bool bBlankHundred = false;
    bool bBlankTen = false;
    bool bNegativeTen = false;
    bool bNegativeHundred = false;
    bool bDisplayDigitFour = false;
    bool bDisplayDigitThree = false;
    bool bDisplayDigitTwo = false;
    bool bDisplayDigitOne = false;

    switch (eStringPosition)
    {
        case  TOPLEFT1 :
            nXPos = 40;
            nYPos = 0;
            nDigits = 3;
            nSize = SMALL;
            break;
        case  TOPLEFT2 :
            nXPos = 40;
            nYPos = 35;
            nDigits = 3;
            nSize = SMALL;
            break;
        case  TOPLEFT3 :
            nXPos = 40;
            nYPos = 70;
            nDigits = 3;
            nSize = SMALL;
            break;
        case  TOPRIGHT :
            nXPos = 140;
            nYPos = 45;
            nDigits = 3;
            nSize = MEDIUM;
            break;
        case  BOTTOM :
            nXPos = 20;
            nYPos = 125;
            nDigits = 4;
            nSize = LARGE;
            break;
        default   :
            nXPos = 40;
            nYPos = 0;
            nDigits = 3;
            nSize = SMALL;
            break;
    }        
    
    // Seperate the incoming value into its digits
    // Check if the value on the display has changed for each digit
    // so it doesn't need to be display again if not. Saves valuable UART time
    if(nDigits == 4)
    {
        nThousand = (nValue - (nValue%1000)) / 1000;
        nThousandOnLCD = (nValueOnLCD - (nValueOnLCD%1000)) / 1000;
        if((nThousand - nThousandOnLCD) != 0) bDisplayDigitFour = true;
    }
    nHundred = ((nValue%1000)-(nValue%100))/100;
    nHundredOnLCD = ((nValueOnLCD%1000)-(nValueOnLCD%100))/100;
    if((nHundred - nHundredOnLCD) != 0) bDisplayDigitThree = true;

    nTen = ((nValue%100)-(nValue%10))/10;
    nTenOnLCD = ((nValueOnLCD%100)-(nValueOnLCD%10))/10;
    if((nTen - nTenOnLCD) != 0) bDisplayDigitTwo = true;

    nSingle = nValue%10;
    nSingleOnLCD = nValueOnLCD%10;
    if((nSingle - nSingleOnLCD) != 0) bDisplayDigitOne = true;
    
    // Check where the "-" bmp needs to be positioned and remove negative value
    if(nValue < -9)
    {
        bNegativeHundred = true;
    }
    else if(nValue < 0) 
    {
        bNegativeTen = true;
    }    

    // Check where the blanks need to be positioned
    if(abs(nValue) < 1000) bBlankThousand = true;
    if(abs(nValue) < 100) bBlankHundred = true;
    if(abs(nValue) < 10) bBlankTen = true;
   
    if(nSize == SMALL)
    {
        if((bDisplayDigitThree == true) || (bColourChanged == true) || (bNegativeChanged == true))
        {
            if(bBlankHundred)
            { 
                LCDuSDImage(nXPos, nYPos, BLANKSMALL);
                if(bNegativeHundred)
                {
                    LCDuSDImage(nXPos+10, nYPos, MINUSSMALLBLUE);
                }
            }
            else
            {    
                LCDuSDImage(nXPos, nYPos, nSmallFont[eStringColour][abs(nHundred)]);
            }
        }        
        if((bDisplayDigitTwo == true) || (bColourChanged == true) || (bNegativeChanged == true))
        {
            if(bBlankTen)
            {
                LCDuSDImage(nXPos+20, nYPos, BLANKSMALL);
                if(bNegativeTen)
                { 
                    LCDuSDImage(nXPos+30, nYPos, MINUSSMALLBLUE);
                }
            }
            else
            {    
                LCDuSDImage(nXPos+20, nYPos, nSmallFont[eStringColour][abs(nTen)]);
            }
        }        
        if((bDisplayDigitOne == true) || (bColourChanged == true) || (bNegativeChanged == true))
        {
            LCDuSDImage(nXPos+40, nYPos, nSmallFont[eStringColour][abs(nSingle)]);
        }    
    }        
   
    if(nSize == MEDIUM)
    {
        if((bDisplayDigitThree == true) || (bColourChanged == true))
        {
            if(bBlankHundred)
            { 
                LCDuSDImage(nXPos, nYPos, BLANKMEDIUM);
            }
            else
            {    
                LCDuSDImage(nXPos, nYPos, nMediumFont[eStringColour][nHundred]);
            }
        }        
        if((bDisplayDigitTwo == true) || (bColourChanged == true))
        {
            if(bBlankTen)
            { 
                LCDuSDImage(nXPos+40, nYPos, BLANKMEDIUM);
            }
            else
            {    
                LCDuSDImage(nXPos+40, nYPos, nMediumFont[eStringColour][nTen]);
            }
        }        
        if((bDisplayDigitOne == true) || (bColourChanged == true))
        {
            LCDuSDImage(nXPos+80, nYPos, nMediumFont[eStringColour][nSingle]);
        }    
    }        
    
    if(nSize == LARGE)
    {
        if((bDisplayDigitFour == true) || (bColourChanged == true))
        {
            if(bBlankThousand)
            { 
                LCDuSDImage(nXPos, nYPos, BLANKLARGE);
            }
            else
            {    
                LCDuSDImage(nXPos, nYPos, nLargeFont[eStringColour][nThousand]);
            }
        }        
        if((bDisplayDigitThree == true) || (bColourChanged == true))
        {
            if(bBlankHundred)
            { 
                LCDuSDImage(nXPos+60, nYPos, BLANKLARGE);
            }
            else
            {    
                LCDuSDImage(nXPos+60, nYPos, nLargeFont[eStringColour][nHundred]);
            }
        }      
        if((bDisplayDigitTwo == true) || (bColourChanged == true))
        {
            if(bBlankTen)
            { 
                LCDuSDImage(nXPos+120, nYPos, BLANKLARGE);
            }
            else
            {    
                LCDuSDImage(nXPos+120, nYPos, nLargeFont[eStringColour][nTen]);
            }
        }       
        if((bDisplayDigitOne == true) || (bColourChanged == true))
        {
            LCDuSDImage(nXPos+180, nYPos, nLargeFont[eStringColour][nSingle]);
        }    
    }        
}    
//******************************************************************************************************
// Touch screen sub functions
//******************************************************************************************************

// Send touch mode (WAIT, PRESS, RELEASE or MOVE)
void LCDTouchMode(char mode)
{ 
    SendData[0] = GETTOUCH;
    SendData[1] = mode;

    SendDataBuffer(SendData, 2);
}

// Get the touch coordinates
void LCDGetTouch(void)
{
    unsigned char x1, x2, y1, y2;
    SendData[0] = GETTOUCH;
    SendData[1] = GETPOSITION;
    
    SendDataBuffer(SendData, 2);
    GetDataBuffer(ReceiveData, 4);
    x1 = ReceiveData[0];
    x2 = ReceiveData[1];
    y1 = ReceiveData[2];
    y2 = ReceiveData[3];
    
    TouchX = (x1 << 8) + x2;
    TouchY = (y1 << 8) + y2;

//#ifdef DEBUGMODE
    //printf ("x =  %d\n" , TouchX);
    //printf ("y =  %d\n\n" , TouchY);
//#endif
}

// Get the touch screen status
char* LCDTouchStatus(void)
{
    SendData[0] = GETTOUCH;
    SendData[1] = STATUS;
    
    ClearDataBuffer();
    SendDataBuffer(SendData, 2);
    GetDataBuffer(ReceiveData, 4);
    //status = ReceiveData[0];
    return ReceiveData;
}

// wait until touch within a delay in milliseconds and return ack (06) when touched 
// or nak (15) when timed out
char LCDWaitTouch(int delay)
{ 
    SendData[0] = WAITTOUCH;

    SendData[1] = (delay >> 8) & 0xFF;
    SendData[2] = delay & 0xFF;

    SendDataBuffer(SendData, 3);
    GetDataBuffer(ReceiveData, 1);
    return ReceiveData[0];
}

// define touch area
void LCDSetTouch(int x1, int y1 , int x2, int y2)
{ 
    SendData[0] = SETTOUCH;

    SendData[1] = (x1 >> 8) & 0xFF;
    SendData[2] = x1 & 0xFF;

    SendData[3] = (y1 >> 8) & 0xFF;
    SendData[4] = y1 & 0xFF;

    SendData[5] = (x2 >> 8) & 0xFF;
    SendData[6] = x2 & 0xFF;

    SendData[7] = (y2 >> 8) & 0xFF;
    SendData[8] = y2 & 0xFF;

    SendDataBuffer(SendData, 9);
}

// Find out the area being touched
unsigned char LCDGridPosition(void)
{
    LCDGetTouch();
    // Top part of Grid
    if (TouchY > 10 && TouchY < 110)
    {
        if (TouchX > 10 && TouchX < 100) return 1;
        if (TouchX > 120 && TouchX < 205) return 2;
        if (TouchX > 225 && TouchX < 330) return 3;
    }    
    // Bottom part of Grid
    if (TouchY > 130 && TouchY < 230)
    {
        if (TouchX > 10 && TouchX < 100) return 4;
        if (TouchX > 120 && TouchX < 205) return 5;
        if (TouchX > 225 && TouchX < 330) return 6;
    }
    return 0;
}    

//******************************************************************************************************
// There is no way to have the uLCD-32PT trigger an interrupt when touched. 
// This function polls the screen and waits for a touch to regiester
// Actually waits for a TouchScreen release!
//******************************************************************************************************
/*void LCDPauseUntilTouch(int *x, int *y)
{ 
    char TouchStatus = 0;                       //Initalise the TouchStatus as 0 = no touch activity

    do
    {
        TouchStatus = LCDTouchStatus();           //Get the touchscreen status
        wait(0.1);
    }while (TouchStatus != 2);
    
    SendData[0] = GETTOUCH;
    SendData[1] = GETPOSITION;
    
    LCDGetTouchXY(SendData, 2, x, y);
}

// read screen info and populate data
void LCDGetTouchXY(char *command, int number, int *x, int *y)
{ 
    int i, temp = 0, resp = 0;

    //freeBUFFER();

    for (i = 0; i < number; i++)
    {
        SendDataBuffer(SendData[i]);    // send all chars to serial port

    while (!_cmd.readable()) wait_ms(TEMPO);               // wait for screen answer

    while (_cmd.readable()) {
        temp = _cmd.getc();
        response[resp++] = (char)temp;
    }

#if DEBUGMODE
    pc.printf("   Answer received %d : 0x%02X 0x%02X 0x%02X 0x%02X\n\r", resp, response[0], response[1], response[2], response[3]);
#endif

    switch (resp)
    {
        case 4 :                                                              // if OK populate data
            *x = ((response[0]<<8)+ response[1]) * (response[0] != 0xFF);
            *y = ((response[2]<<8)+ response[3]) * (response[2] != 0xFF);
            break;
        default :
            *x = -1;
            *y = -1;
            break;
    }

#if DEBUGMODE
    pc.printf("   X,Y : %03d,%03d\n\r", *x, *y);
#endif

}*/

// *****************************************************************************
// void UARTTxBuffer(char *buffer, UINT32 size)
// *****************************************************************************
void SendDataBuffer(char *buffer, UINT8 size)
{
    char loop = 0;
    char showbuffer = 0;
    
/*
#ifdef __DEBUG
    MYDEBUG("Command send (0x");
    while (loop < size)
    {
        showbuffer = buffer[loop];
        MYDEBUG("%02X", showbuffer);
        loop++;
    }    
    MYDEBUG(")\nCommand size (%d) .\n", size);
#endif
*/
    while(size)
    {
        while(!UARTTransmitterIsReady(UART1));
        
        UARTSendDataByte(UART1, *buffer);

        buffer++;
        size--;
    }
    while(!UARTTransmissionHasCompleted(UART1));
}

// *****************************************************************************
// UINT32 GetDataBuffer(char *buffer, UINT32 max_size)
// *****************************************************************************
char GetDataBuffer(char *buffer, UINT8 max_size)
{
    UINT8 num_char;

    num_char = 0;

    char loop = 0;
    char showbuffer = 0;

    while(num_char < max_size)
    {
        char character;

        while(!UARTReceivedDataIsAvailable(UART1));

        character = UARTGetDataByte(UART1);

        if(character == '\r')
            break;

        buffer[num_char] = character;

        num_char++;
    }

/*
#ifdef __DEBUG
    MYDEBUG("Response received (0x");
    while (loop < num_char)
    {
        showbuffer = buffer[loop];
        MYDEBUG("%02X", showbuffer);
        loop++;
    }    
    MYDEBUG(")\nCommand size (%d) .\n", num_char);
#endif
*/
    return num_char;

}

// *****************************************************************************
// void ClearDataBuffer(void)
// *****************************************************************************
void ClearDataBuffer(void)
{
    char trash = 0;
    
    while(UARTReceivedDataIsAvailable(UART1))
    {
        trash = UARTGetDataByte(UART1);
    }
}

// Load Array with font bmp addresses
char LoadFontBMPAddress(void)
{
    nSmallFont[REDTEXT][0] = ZEROSMALLRED;
    nSmallFont[REDTEXT][1] = ONESMALLRED;
    nSmallFont[REDTEXT][2] = TWOSMALLRED;
    nSmallFont[REDTEXT][3] = THREESMALLRED;
    nSmallFont[REDTEXT][4] = FOURSMALLRED;
    nSmallFont[REDTEXT][5] = FIVESMALLRED;
    nSmallFont[REDTEXT][6] = SIXSMALLRED;
    nSmallFont[REDTEXT][7] = SEVENSMALLRED;
    nSmallFont[REDTEXT][8] = EIGHTSMALLRED;
    nSmallFont[REDTEXT][9] = NINESMALLRED;
    nSmallFont[REDTEXT][10] = DPSMALLRED;
    //nSmallFont[REDTEXT][11] = MINUSSMALLRED;

    nSmallFont[GREENTEXT][0] = ZEROSMALLGREEN;
    nSmallFont[GREENTEXT][1] = ONESMALLGREEN;
    nSmallFont[GREENTEXT][2] = TWOSMALLGREEN;
    nSmallFont[GREENTEXT][3] = THREESMALLGREEN;
    nSmallFont[GREENTEXT][4] = FOURSMALLGREEN;
    nSmallFont[GREENTEXT][5] = FIVESMALLGREEN;
    nSmallFont[GREENTEXT][6] = SIXSMALLGREEN;
    nSmallFont[GREENTEXT][7] = SEVENSMALLGREEN;
    nSmallFont[GREENTEXT][8] = EIGHTSMALLGREEN;
    nSmallFont[GREENTEXT][9] = NINESMALLGREEN;
    nSmallFont[GREENTEXT][10] = DPSMALLGREEN;

    nSmallFont[BLUETEXT][0] = ZEROSMALLBLUE;
    nSmallFont[BLUETEXT][1] = ONESMALLBLUE;
    nSmallFont[BLUETEXT][2] = TWOSMALLBLUE;
    nSmallFont[BLUETEXT][3] = THREESMALLBLUE;
    nSmallFont[BLUETEXT][4] = FOURSMALLBLUE;
    nSmallFont[BLUETEXT][5] = FIVESMALLBLUE;
    nSmallFont[BLUETEXT][6] = SIXSMALLBLUE;
    nSmallFont[BLUETEXT][7] = SEVENSMALLBLUE;
    nSmallFont[BLUETEXT][8] = EIGHTSMALLBLUE;
    nSmallFont[BLUETEXT][9] = NINESMALLBLUE;
    nSmallFont[BLUETEXT][10] = DPSMALLBLUE;
    nSmallFont[BLUETEXT][11] = MINUSSMALLBLUE;

    nMediumFont[REDTEXT][0] = ZEROMEDIUMRED;
    nMediumFont[REDTEXT][1] = ONEMEDIUMRED;
    nMediumFont[REDTEXT][2] = TWOMEDIUMRED;
    nMediumFont[REDTEXT][3] = THREEMEDIUMRED;
    nMediumFont[REDTEXT][4] = FOURMEDIUMRED;
    nMediumFont[REDTEXT][5] = FIVEMEDIUMRED;
    nMediumFont[REDTEXT][6] = SIXMEDIUMRED;
    nMediumFont[REDTEXT][7] = SEVENMEDIUMRED;
    nMediumFont[REDTEXT][8] = EIGHTMEDIUMRED;
    nMediumFont[REDTEXT][9] = NINEMEDIUMRED;
    //nMediumFont[REDTEXT][10] = DPMEDIUMRED;
    //nMediumFont[REDTEXT][11] = MINUSMEDIUMRED;

    nMediumFont[GREENTEXT][0] = ZEROMEDIUMGREEN;
    nMediumFont[GREENTEXT][1] = ONEMEDIUMGREEN;
    nMediumFont[GREENTEXT][2] = TWOMEDIUMGREEN;
    nMediumFont[GREENTEXT][3] = THREEMEDIUMGREEN;
    nMediumFont[GREENTEXT][4] = FOURMEDIUMGREEN;
    nMediumFont[GREENTEXT][5] = FIVEMEDIUMGREEN;
    nMediumFont[GREENTEXT][6] = SIXMEDIUMGREEN;
    nMediumFont[GREENTEXT][7] = SEVENMEDIUMGREEN;
    nMediumFont[GREENTEXT][8] = EIGHTMEDIUMGREEN;
    nMediumFont[GREENTEXT][9] = NINEMEDIUMGREEN;
    //nMediumFont[GREENTEXT][10] = 0;//DPMEDIUMGREEN;
    //nMediumFont[GREENTEXT][11] = 0;//MINUSMEDIUMGREEN;

    nMediumFont[BLUETEXT][0] = ZEROMEDIUMBLUE;
    nMediumFont[BLUETEXT][1] = ONEMEDIUMBLUE;
    nMediumFont[BLUETEXT][2] = TWOMEDIUMBLUE;
    nMediumFont[BLUETEXT][3] = THREEMEDIUMBLUE;
    nMediumFont[BLUETEXT][4] = FOURMEDIUMBLUE;
    nMediumFont[BLUETEXT][5] = FIVEMEDIUMBLUE;
    nMediumFont[BLUETEXT][6] = SIXMEDIUMBLUE;
    nMediumFont[BLUETEXT][7] = SEVENMEDIUMBLUE;
    nMediumFont[BLUETEXT][8] = EIGHTMEDIUMBLUE;
    nMediumFont[BLUETEXT][9] = NINEMEDIUMBLUE;
    //nMediumFont[BLUETEXT][10] = DPMEDIUMBLUE;
    //nMediumFont[BLUETEXT][11] = MINUSMEDIUMBLUE;

    nLargeFont[REDTEXT][0] = ZEROLARGERED;
    nLargeFont[REDTEXT][1] = ONELARGERED;
    nLargeFont[REDTEXT][2] = TWOLARGERED;
    nLargeFont[REDTEXT][3] = THREELARGERED;
    nLargeFont[REDTEXT][4] = FOURLARGERED;
    nLargeFont[REDTEXT][5] = FIVELARGERED;
    nLargeFont[REDTEXT][6] = SIXLARGERED;
    nLargeFont[REDTEXT][7] = SEVENLARGERED;
    nLargeFont[REDTEXT][8] = EIGHTLARGERED;
    nLargeFont[REDTEXT][9] = NINELARGERED;
    //nLargeFont[REDTEXT][10] = DPLARGERED;
    //nLargeFont[REDTEXT][11] = MINUSLARGERED;

    nLargeFont[GREENTEXT][0] = ZEROLARGEGREEN;
    nLargeFont[GREENTEXT][1] = ONELARGEGREEN;
    nLargeFont[GREENTEXT][2] = TWOLARGEGREEN;
    nLargeFont[GREENTEXT][3] = THREELARGEGREEN;
    nLargeFont[GREENTEXT][4] = FOURLARGEGREEN;
    nLargeFont[GREENTEXT][5] = FIVELARGEGREEN;
    nLargeFont[GREENTEXT][6] = SIXLARGEGREEN;
    nLargeFont[GREENTEXT][7] = SEVENLARGEGREEN;
    nLargeFont[GREENTEXT][8] = EIGHTLARGEGREEN;
    nLargeFont[GREENTEXT][9] = NINELARGEGREEN;
    //nLargeFont[GREENTEXT][10] = 0;//DPLARGEGREEN;
    //nLargeFont[GREENTEXT][11] = 0;//MINUSLARGEGREEN;

    nLargeFont[BLUETEXT][0] = ZEROLARGEBLUE;
    nLargeFont[BLUETEXT][1] = ONELARGEBLUE;
    nLargeFont[BLUETEXT][2] = TWOLARGEBLUE;
    nLargeFont[BLUETEXT][3] = THREELARGEBLUE;
    nLargeFont[BLUETEXT][4] = FOURLARGEBLUE;
    nLargeFont[BLUETEXT][5] = FIVELARGEBLUE;
    nLargeFont[BLUETEXT][6] = SIXLARGEBLUE;
    nLargeFont[BLUETEXT][7] = SEVENLARGEBLUE;
    nLargeFont[BLUETEXT][8] = EIGHTLARGEBLUE;
    nLargeFont[BLUETEXT][9] = NINELARGEBLUE;
    //nLargeFont[BLUETEXT][10] = DPLARGEBLUE;
    //nLargeFont[BLUETEXT][11] = MINUSLARGEBLUE;
}
